// NMI's Java Code Viewer 6.0a
// www.trinnion.com/javacodeviewer

// Registered to Evaluation Copy                                      
// Generated PGFZKD AyTB 14 2007 15:43:34 

//source File Name:   KDJ.java

package com.chiyou.common.indicator;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chiyou.common.models.HisData;

public class KDJ extends IndicatorBase {

	private List<HisData> m_kDatas;

	public List<HisData> getM_kDatas() {
		return m_kDatas;
	}

	public void setM_kDatas(List<HisData> m_kDatas) {
		this.m_kDatas = m_kDatas;
	}

	private final int m_iParam[] = { 9, 3, 3 };

	public KDJ() {
		super.m_strIndicatorName = "KDJ(" + m_iParam[0] + "," + m_iParam[1] + "," + m_iParam[2] + ")";
		super.m_strParamName = (new String[] { "K", "D", "J" });
		super.m_iPrecision = 2;
	}

	public KDJ(IndicatorPos pos, int Precision) {
		super(pos, Precision);
		super.m_strIndicatorName = "KDJ(" + m_iParam[0] + "," + m_iParam[1] + "," + m_iParam[2] + ")";
		super.m_strParamName = (new String[] { "K", "D", "J" });
		super.m_iPrecision = 2;
	}

	// public void Paint(Graphics g, Rectangle rc, KLineData data[]) {
	// if(data == null || data.length == 0)
	// return;
	// super.Paint(g, rc, data);
	// Calculate();
	// super.m_max = 0.0F;
	// super.m_min = 10000F;
	// for(int i = 0; i < 3; i++)
	// GetValueMaxMin(super.m_data[i], m_iParam[i]);
	//
	// DrawCoordinate(g, 2);
	// for(int i = 0; i < m_iParam.length; i++)
	// DrawLine(g, super.m_data[i], m_iParam[i],
	// HQApplet.rhColor.clIndicator[i]);
	//
	// }

	public void Calculate() {
		super.m_data = new float[3][this.m_kDatas.size()];
		String code = getM_kDatas().get(0).getCODE();
		int n1 = m_iParam[0];
		int n2 = m_iParam[1];
		int n3 = m_iParam[2];
		// if(m_kData == null || n1 > m_kData.length || n1 < 1)
		// return;
		float kvalue[] = new float[this.m_kDatas.size()];
		float dvalue[] = new float[this.m_kDatas.size()];
		float jvalue[] = new float[this.m_kDatas.size()];
		n2 = n2 > 0 ? n2 : 3;
		n3 = n3 > 0 ? n3 : 3;
		float maxhigh = m_kDatas.get(n1 - 1).getHIGH();
		float minlow = m_kDatas.get(n1 - 1).getLOW();
		for (int j = n1 - 1; j >= 0; j--) {
			if (maxhigh < m_kDatas.get(j).getHIGH())
				maxhigh = m_kDatas.get(j).getHIGH();
			if (minlow < m_kDatas.get(j).getLOW())
				minlow = m_kDatas.get(j).getLOW();
		}

		float rsv;
		if (maxhigh <= minlow)
			rsv = 50F;
		else
			rsv = ((m_kDatas.get(n1 - 1).getCLOSE() - minlow) / (maxhigh - minlow)) * 100F;
		float prersv;
		kvalue[n1 - 1] = dvalue[n1 - 1] = jvalue[n1 - 1] = prersv = rsv;
		for (int i = 0; i < n1; i++) {
			kvalue[i] = 0.0F;
			dvalue[i] = 0.0F;
			jvalue[i] = 0.0F;
		}

		for (int i = n1; i < m_kDatas.size(); i++) {
			maxhigh = m_kDatas.get(i).getHIGH();
			minlow = m_kDatas.get(i).getLOW();
			for (int j = i - 1; j > i - n1; j--) {
				if (maxhigh < m_kDatas.get(j).getHIGH())
					maxhigh = m_kDatas.get(j).getHIGH();
				if (minlow > m_kDatas.get(j).getLOW())
					minlow = m_kDatas.get(j).getLOW();
			}

			if (maxhigh <= minlow) {
				rsv = prersv;
			} else {
				prersv = rsv;
				rsv = ((m_kDatas.get(i).getCLOSE() - minlow) / (maxhigh - minlow)) * 100F;
			}
			kvalue[i] = (kvalue[i - 1] * (float) (n2 - 1)) / (float) n2 + rsv / (float) n2;
			dvalue[i] = kvalue[i] / (float) n3 + (dvalue[i - 1] * (float) (n3 - 1)) / (float) n3;
			jvalue[i] = 3F * kvalue[i] - 2.0F * dvalue[i];

			DecimalFormat decimalFormat = new DecimalFormat(".00");

			float d = Float.valueOf(decimalFormat.format(dvalue[i]));
			float k = Float.valueOf(decimalFormat.format(kvalue[i]));
			float j = Float.valueOf(decimalFormat.format(jvalue[i]));

			Map map = new HashMap();
			map.put("k", k);
			map.put("d", d);
			map.put("j", j);
			map.put("date", date2String(m_kDatas.get(i).getTIME(), "YYYY-MM-dd HH:mm:ss"));
			this.addIndicators(map);
		}
	}

	public static String date2String(Date time, String dataFormat) {
		SimpleDateFormat s = new SimpleDateFormat(dataFormat);
		String str = s.format(time);
		return str;
	}

	public int isBuyOrSell() {
		// TODO Auto-generated method stub
		List kdjList = this.getIndicators();
		int isUp = 0;
		int lastStatus = 0;

		for (int i = 1; i < kdjList.size(); i++) {
			Map kdjMap = (Map) kdjList.get(i);
			// Map lastKdjMap = (Map) kdjList.get(i - 1);
			// Map nextKdjMap = (Map) kdjList.get(getNextIndex(i,
			// kdjList.size()));
			float k = Float.parseFloat("" + kdjMap.get("k"));
			float d = Float.parseFloat("" + kdjMap.get("d"));
			float j = Float.parseFloat("" + kdjMap.get("j"));

			// float lk = Float.parseFloat("" + lastKdjMap.get("k"));
			// float ld = Float.parseFloat("" + lastKdjMap.get("d"));
			// float lj = Float.parseFloat("" + lastKdjMap.get("j"));
			//
			// float nk = Float.parseFloat("" + nextKdjMap.get("k"));
			// float nd = Float.parseFloat("" + nextKdjMap.get("d"));
			// float nj = Float.parseFloat("" + nextKdjMap.get("j"));
			int status = 0;
			if (k > d) {
				status = 1;
			}

			if (d > k) {
				status = -1;
			}

			if (status == 1) {
				// System.out.println("买单");
				isUp = 10;
				if (lastStatus == -1) {
					System.out.println("kdj买单" + kdjMap.get("date"));
					isUp = 1;
				}
			}

			if (status == -1) {
				//
				isUp = -10;

				if (lastStatus == 1) {
					System.out.println("kdj卖单" + kdjMap.get("date"));
					isUp = -1;
				}
			}

			// System.out.println(i);

			if (i > 1) {
				lastStatus = status;
			}
			System.out.println(" k: " + k + " d: " + d + " j : " + j);
		}
		return isUp;
	}

	// @Override
	// public boolean isBuyTime() {
	// // TODO Auto-generated method stub
	// List kdjList = this.getIndicators();
	// int isUp = 0;
	// int lastStatus =0;
	// for (int i = 1; i < kdjList.size() - 1; i++) {
	// Map kdjMap = (Map) kdjList.get(i);
	// Map lastKdjMap = (Map) kdjList.get(i - 1);
	// Map nextKdjMap = (Map) kdjList.get(getNextIndex(i, kdjList.size()));
	// float k = Float.parseFloat("" + kdjMap.get("k"));
	// float d = Float.parseFloat("" + kdjMap.get("d"));
	// float j = Float.parseFloat("" + kdjMap.get("j"));
	//
	// float lk = Float.parseFloat("" + lastKdjMap.get("k"));
	// float ld = Float.parseFloat("" + lastKdjMap.get("d"));
	// float lj = Float.parseFloat("" + lastKdjMap.get("j"));
	//
	// float nk = Float.parseFloat("" + nextKdjMap.get("k"));
	// float nd = Float.parseFloat("" + nextKdjMap.get("d"));
	// float nj = Float.parseFloat("" + nextKdjMap.get("j"));
	//
	// if ((lk < ld && lj < ld && k > d && j > d && isUp <= 0) || (k < d && j <
	// d && nk > d && nj > d && isUp <= 0)) {
	// isUp = 1;
	// } else {
	// isUp = 0;
	// }
	// }
	// if (isUp > 0)
	// return true;
	// return false;
	// }

	private int getNextIndex(int i, int size) {
		return i + 1 < size ? i + 1 : i;
	}

	public boolean isSellTime() {
		// TODO Auto-generated method stub
		List kdjList = this.getIndicators();
		int isDown = 0;
		for (int i = 1; i < kdjList.size() - 1; i++) {
			Map kdjMap = (Map) kdjList.get(i);
			Map lastKdjMap = (Map) kdjList.get(i - 1);
			Map nextKdjMap = (Map) kdjList.get(getNextIndex(i, kdjList.size()));
			float k = Float.parseFloat("" + kdjMap.get("k"));
			float d = Float.parseFloat("" + kdjMap.get("d"));
			float j = Float.parseFloat("" + kdjMap.get("j"));

			float lk = Float.parseFloat("" + lastKdjMap.get("k"));
			float ld = Float.parseFloat("" + lastKdjMap.get("d"));
			float lj = Float.parseFloat("" + lastKdjMap.get("j"));

			float nk = Float.parseFloat("" + nextKdjMap.get("k"));
			float nd = Float.parseFloat("" + nextKdjMap.get("d"));
			float nj = Float.parseFloat("" + nextKdjMap.get("j"));
			if ((lk > ld && lj > ld && k < d && j < d && isDown <= 0) || (k > d && j > d && nk < d && nj < d && isDown <= 0)) {
				isDown = 1;
			} else {
				isDown = 0;
			}

		}
		if (isDown > 0)
			return true;
		return false;
	}

}
